package com.canbot.u05.network

import android.util.Log
import com.alibaba.fastjson.JSON
import com.canbot.u05.activity.logd
import com.canbot.u05.bean.JsonResponse
import com.canbot.u05.bean.dance.*
import com.canbot.u05.socket.IpMsgConst
import okhttp3.OkHttpClient
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory
import java.util.concurrent.TimeUnit


/**
 * Created by tao.liu on 2019/6/28.
 * 舞蹈管理数据接口工具类
 */

object DanceUtil {
        private val TAG = "DanceUtil"
        private val TIMEOUT = 60//超时时间60s

        /**
         * 设置超时
         *
         * @return
         */
        private fun setTimeOut(): OkHttpClient {
                return OkHttpClient.Builder()
                        .connectTimeout(TIMEOUT.toLong(), TimeUnit.SECONDS)
                        .writeTimeout(TIMEOUT.toLong(), TimeUnit.SECONDS)
                        .readTimeout(TIMEOUT.toLong(), TimeUnit.SECONDS)
                        .build()

        }

        private fun retrofit(): Retrofit {
                return Retrofit.Builder()
                        .baseUrl("http://" + IpMsgConst.CHEST_SOCKET_HOST + ":7755")
                        .client(setTimeOut())
                        .addConverterFactory(GsonConverterFactory.create())
                        .build()
        }

        /**
         * 请求头部http
         */
        private fun retrofit2(): Retrofit {
                return Retrofit.Builder()
                        .baseUrl("http://" + IpMsgConst.HEAD_SOCKET_HOST + ":7755")
                        .client(setTimeOut())
                        .addConverterFactory(GsonConverterFactory.create())
                        .build()
        }

        /**
         * 查询所有舞蹈数据
         */
        fun queryAllDance(danceCallBack: DanceCallBack<DanceListInfo>) {
                val queryAllDace = retrofit().create(DanceApi::class.java).queryAllDance("queryAll")
                queryAllDace.enqueue(object : Callback<DanceListInfo> {
                        override fun onFailure(call: Call<DanceListInfo>, t: Throwable) {
                                Log.d(TAG, "onFailure ${t.message}")
                                danceCallBack.onFailure()
                        }

                        override fun onResponse(call: Call<DanceListInfo>, response: Response<DanceListInfo>) {
                                val danceBean = response.body()!!
                                Log.d(TAG, "allDance:${danceBean}")
                                danceCallBack.onSuccess(danceBean)
                        }

                })
        }

        /**
         * 修改舞蹈的使用禁用
         */
        fun editEnabled(id: Int, enabled: Boolean, danceCallBack: DanceCallBack<ResponseInfo>) {
                val editEnabledRequestBody = EditEnabledRequestBody(id, enabled)
                val jsonString = JSON.toJSONString(editEnabledRequestBody)
                logd("请求参数editEnabled()：" + jsonString)
                val editEnabled = retrofit().create(DanceApi::class.java).editEnabled(jsonString)
                editEnabled.enqueue(object : Callback<ResponseInfo> {
                        override fun onFailure(call: Call<ResponseInfo>, t: Throwable) {
                                Log.d(TAG, "onFailure ${t.message}")
                                danceCallBack.onFailure()
                        }

                        override fun onResponse(call: Call<ResponseInfo>, response: Response<ResponseInfo>) {
                                val body = response.body()!!
                                logd("修改使用禁用结果：" + body)
                                danceCallBack.onSuccess(body)
                        }
                })
        }

        /**
         * 获取舞蹈默认配图
         */
        fun defaultPic(danceCallBack: DanceCallBack<DefaultPicBean>) {
                val defaultPic = retrofit().create(DanceApi::class.java).defaultPic("defaultPic")
                defaultPic.enqueue(object : Callback<DefaultPicBean> {
                        override fun onFailure(call: Call<DefaultPicBean>, t: Throwable) {
                                Log.d(TAG, "onFailure ${t.message}")
                                danceCallBack.onFailure()
                        }

                        override fun onResponse(call: Call<DefaultPicBean>, response: Response<DefaultPicBean>) {
                                logd("获取图片结果：" + response.body())
                                danceCallBack.onSuccess(response.body()!!)
                        }

                })
        }

        /**
         * 修改舞蹈信息
         */
        fun updateDanceInfo(json: String, danceCallBack: DanceCallBack<ResponseInfo>) {
                val updateDanceInfo = retrofit().create(DanceApi::class.java).updateDanceInfo(json)
                updateDanceInfo.enqueue(object : Callback<ResponseInfo> {
                        override fun onFailure(call: Call<ResponseInfo>, t: Throwable) {
                                Log.d(TAG, "onFailure ${t.message}")
                                danceCallBack.onFailure()
                        }

                        override fun onResponse(call: Call<ResponseInfo>, response: Response<ResponseInfo>) {
                                logd("修改舞蹈信息结果：" + response.body())
                                danceCallBack.onSuccess(response.body()!!)
                        }

                })
        }

        /**
         * 提交舞蹈动作到胸口
         */
        fun addDance(json: String,danceCallBack: DanceCallBack<ResponseInfo>){
                val addDance = retrofit().create(DanceApi::class.java).addDance(json)
                addDance.enqueue(object :Callback<ResponseInfo>{
                        override fun onFailure(call: Call<ResponseInfo>, t: Throwable) {
                                Log.d(TAG, "onFailure ${t.message}")
                                danceCallBack.onFailure()
                        }

                        override fun onResponse(call: Call<ResponseInfo>, response: Response<ResponseInfo>) {
                                logd("提交舞蹈动作结果：" + response.body())
                                danceCallBack.onSuccess(response.body()!!)
                        }

                })
        }

        /**
         * 删除一个舞蹈 根据id
         */
        fun delDance(id:String,danceCallBack: DanceCallBack<ResponseInfo>){
                val delDance = retrofit().create(DanceApi::class.java).delDance(id)
                delDance.enqueue(object :Callback<ResponseInfo>{
                        override fun onFailure(call: Call<ResponseInfo>, t: Throwable) {
                                Log.d(TAG, "onFailure ${t.message}")
                                danceCallBack.onFailure()
                        }

                        override fun onResponse(call: Call<ResponseInfo>, response: Response<ResponseInfo>) {
                                logd("删除舞蹈动作结果：" + response.body())
                                danceCallBack.onSuccess(response.body()!!)
                        }

                })
        }
        /**
         * 获取舞蹈音频
         */
        fun getAudioList(path: String, danceCallBack: DanceCallBack<JsonResponse>) {
                val audioList = retrofit2().create(DanceApi::class.java).getAudioList(path)
                audioList.enqueue(object : Callback<JsonResponse> {
                        override fun onFailure(call: Call<JsonResponse>, t: Throwable) {
                                Log.d(TAG, "onFailure ${t.message}")
                                danceCallBack.onFailure()
                        }

                        override fun onResponse(call: Call<JsonResponse>, response: Response<JsonResponse>) {
                                logd("获取舞蹈音频结果：" + response.body())
                                if (response.body() != null) {
                                        danceCallBack.onSuccess(response.body()!!)
                                } else {
                                        danceCallBack.onFailure()
                                }

                        }

                })
        }

        /**
         * 获取长动作(暂时固定)
         */
        fun getLongActionList(): List<ActionBean> {
                val longActionList = listOf(
                        ActionBean(10236, "海草海草", 52),
                        ActionBean(1006, "最炫民族风", 53),
                        ActionBean(10237, "Despacito", 53),
                        ActionBean(6, "小苹果", 58),
                        ActionBean(10225, "龙拳", 64),
                        ActionBean(10227, "蓝色多瑙河", 79),
                        ActionBean(10235, "无法停止的感觉", 83),
                        ActionBean(10224, "杰克逊舞蹈", 83),
                        ActionBean(10238, "Trouble Maker", 84),
                        ActionBean(10239, "红昭愿", 94)
                )

                return longActionList
        }

        /**
         * 获取短动作(暂时固定)
         */
        fun getShortActionList(): List<ActionBean> {
                val shortActionList = listOf(
                        ActionBean(15001, "Dance01", 3),
                        ActionBean(15002, "Dance02", 6),
                        ActionBean(15003, "Dance03", 9),
                        ActionBean(15004, "Dance04", 12),
                        ActionBean(15005, "Dance05", 15),
                        ActionBean(15006, "Dance06", 18),
                        ActionBean(15007, "Dance07", 21),
                        ActionBean(15008, "Dance08", 24),
                        ActionBean(15009, "Dance09", 27),
                        ActionBean(15010, "Dance10", 30)
                )

                return shortActionList
        }


        interface DanceCallBack<T> {

                fun onSuccess(result: T)

                fun onFailure()
        }
}
